package com.shmilyou.dissertationmanagement.web;
/* Created with 岂止是一丝涟漪     530060499@qq.com    2019/3/7 */

import com.shmilyou.dissertationmanagement.biz.ThesisTypeService;
import com.shmilyou.dissertationmanagement.domain.ThesisType;
import com.shmilyou.dissertationmanagement.tools.Utils;
import com.shmilyou.dissertationmanagement.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;
import java.util.Map;

@Controller
public class ThesisTypeController {

    private final ThesisTypeService thesisTypeService;

    @Autowired
    public ThesisTypeController(ThesisTypeService thesisTypeService) {
        this.thesisTypeService = thesisTypeService;
    }

    @RequestMapping("/thesisType")
    public String index(Map<Object, Object> map) {
        List<ThesisType> entities = thesisTypeService.queryAll();
        map.put("entities", entities);
        return "thesisType";
    }

    @RequestMapping(value = "/thesisType/rm", method = RequestMethod.POST)
    public ResponseEntity<Map<String, Object>> delete(String id) {
        if (Utils.isInteger(id)) {
            ThesisType exit = thesisTypeService.queryById(Integer.parseInt(id));
            if (exit != null) {
                thesisTypeService.delete(exit);
                return WebUtils.success();
            }
        }
        return WebUtils.failure();
    }

    @RequestMapping(value = "/thesisType/add", method = RequestMethod.POST)
    public ResponseEntity<Map<String, Object>> addHandle(String name, String number) {
        if (StringUtils.isEmpty(name) || StringUtils.isEmpty(number)) {
            return WebUtils.failure();
        }
        ThesisType entity = new ThesisType();
        entity.setName(name);
        entity.setNumber(number);
        thesisTypeService.save(entity);
        return WebUtils.success();
    }

    @RequestMapping(value = "/thesisType/add", method = RequestMethod.GET)
    public String add() {
        return "thesisType_add";
    }

    @RequestMapping(value = "/thesisType/edit", method = RequestMethod.GET)
    public String edit(String id, ModelMap map) {
        if (Utils.isInteger(id)) {
            ThesisType entity = thesisTypeService.queryById(Integer.parseInt(id));
            map.put("entity", entity);
            return "thesisType_edit";
        }
        return "error";
    }

    @RequestMapping(value = "/thesisType/edit", method = RequestMethod.POST)
    public ResponseEntity<Map<String, Object>> editHandle(ThesisType entity) {
        thesisTypeService.update(entity);
        return WebUtils.success();
    }
}
